<template>
  <div>
    <ul class="goodsList">
        <li v-for="item in goodsList" :key="item.gid" @click="goGoodsDet(item.gid)">
            <img :src="item.gimg" alt="">
            <span>{{item.gname}}</span>
            <span>{{item.gprice}}元</span>
        </li>
    </ul>
  </div>
</template>

<script>
import getGoods from "@/api/getGoods.js"
export default {
  data () {
    return {
        goodsList:"",
    }
  },

  components: {},
    created(){
        this.getGoods()
    },
  methods: {
      async getGoods(){
          try{
              let p = await getGoods.getGoods(this.$route.params.index)
              if(p.data.flag){
                  this.goodsList = p.data.data 
                  this.getSort(this.goodsList)
              }else{
                  this.$toast.fail("获取数据失败")
              }
          }catch(err){
              this.$toast.fail(err+"")
          }
      },
      goGoodsDet(gid){
          this.$router.push("/goodsDetails/"+gid)
      },
      getSort(list){
          if(this.$store.state.goodsSort.idx == 0){
              
          }else if(this.$store.state.goodsSort.idx == 1){
              for(let i = 0 ; i<list.length-1;i++){
                  for(let j = 0;j<list.length-1-i;j++){
                      if(list[j].gstock > list[j+1].gstock){
                          let a = list[j]
                          list[j] = list[j+1]
                          list[j+1] = a
                      }
                  }
              }
          }else if(this.$store.state.goodsSort.idx == 2){
              for(let i = 0 ; i<list.length-1;i++){
                  for(let j = 0;j<list.length-1-i;j++){
                      if(Math.random()*1 > 0.5){
                          let a = list[j]
                          list[j] = list[j+1]
                          list[j+1] = a
                      }
                  }
              }
          }else if(this.$store.state.goodsSort.idx == 3){
              for(let i = 0 ; i<list.length-1;i++){
                  for(let j = 0;j<list.length-1-i;j++){
                      if(list[j].gprice > list[j+1].gprice){
                          let a = list[j]
                          list[j] = list[j+1]
                          list[j+1] = a
                      }
                  }
              }
          }
      }
  },
  computed:{
      idx(){
          return this.$store.state.goodsSort.idx
      }
  },
  watch:{
      idx:function(){
          this.getGoods()
      }
  }
}
</script>

<style lang="scss" scoped>
.goodsList{
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    li{
        width: 50%;
        height: 202px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        margin-bottom: 22px;
        font-size: 12px;
        span:nth-of-type(1){
            width: 148px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        span:nth-of-type(2){
            font-size: 14px;
        }
        img{
            width: 148px;
            height: 148px;
        }
    }
}
</style>